package com.yzt.zhuju.widget.emticonnskeyboard;

import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.widget.EditText;


import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.file.FileManager;
import com.yzt.zhuju.richtext.CacheEmojiManager;
import com.yzt.zhuju.richtext.entity.EmojiData;
import com.yzt.zhuju.richtext.entity.EmojiFatherBean;
import com.yzt.zhuju.utils.TextSpanUtils;
import com.yzt.zhuju.widget.emticonnskeyboard.interfaces.EmoticonFilter;
import com.yzt.zhuju.widget.emticonnskeyboard.utils.EmoticonsKeyboardUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CustomEmojiFilter extends EmoticonFilter {

    public static final Pattern EMOJI_RANGE = Pattern.compile("(\\[([^\\[\\]].*?)])");
    private int emojiSize = -1;
    private ArrayList<EmojiData> expressionList;

    private void loadData() {
        List<EmojiFatherBean> mDatas = CacheEmojiManager.getInstance().getEmojiList();
        expressionList = new ArrayList<>();
        for (int index = 0; index < mDatas.size(); index++) {
            expressionList.addAll(mDatas.get(0).getExpressionList());
        }
    }

    @Override
    public void filter(EditText editText, CharSequence text, int start, int lengthBefore, int lengthAfter) {
        if (expressionList == null) {
            loadData();
        }

        emojiSize = emojiSize == -1 ? EmoticonsKeyboardUtils.getFontHeight(editText) : emojiSize;
        clearSpan(editText.getText(), 0, text.toString().length());

        Matcher m = EMOJI_RANGE.matcher(text.toString());
        if (m != null) {
            while (m.find()) {
                String emojiStr = m.group();
                int emojiStart = m.start();
                Drawable drawable = getEmojiDrawable(m.group());
                if (drawable != null) {
                    drawable.setBounds(0, 0, emojiSize, emojiSize);
                    CustomEmojiSpan imageSpan = new CustomEmojiSpan(drawable);
                    int length = editText.getText().length();
                    if (TextSpanUtils.isCanSetSpan(emojiStart, emojiStart + emojiStr.length())) {
                        editText.getText().setSpan(imageSpan, emojiStart, emojiStart + emojiStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }
        }
    }

    private void clearSpan(Spannable spannable, int start, int end) {
        if (start == end) {
            return;
        }
        CustomEmojiSpan[] oldSpans = spannable.getSpans(start, end, CustomEmojiSpan.class);
        for (CustomEmojiSpan oldSpan : oldSpans) {
            spannable.removeSpan(oldSpan);
        }
    }


    private Drawable getEmojiDrawable(String faceKey) {
        if (expressionList == null || expressionList.size() == 0) {
            return null;
        }
        for (EmojiData mEmojiData : expressionList) {
            if (faceKey.equals(mEmojiData.getName())) {
                String path = FileManager.getSingleEmoijPath() + File.separator + mEmojiData.getFileName();
                File mFile = new File(path);
                if (mFile.exists()) {
                    return new BitmapDrawable(AppConfig.application.getResources(), BitmapFactory.decodeFile(mFile.getPath()));
                } else {
                    return null;
                }
            }
        }
        return null;
    }
}
